﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        private bool isOK = true;
        public Form1()
        {
            InitializeComponent();
            tbN.Text = "10";
            tbLength.Text = "100";
            tbTh1.Text = "30";
            tbTh2.Text = "20";
            draw.Text = "";
            
                        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isOK) { 
            draw.Text = "绘制中...";
            draw.ForeColor = Color.Red;
            if (graphics == null) graphics = this.CreateGraphics();
            try {
                int n = int.Parse(tbN.Text);
                int leng = int.Parse(tbLength.Text);
                drawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
                draw.Text = "绘制成功！";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            }
            
            
        }

        private Graphics graphics;
        double angle1 = 30;
        double angle2 = 20;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.5;
        double per2 = 0.5;
        Pen SelectColor(String color)
        {
            switch (color)
            {
                case "Blue":
                    return Pens.Blue;
                case "Black":
                    return Pens.Black;
                case "Yellow":
                    return Pens.Yellow;
                case "Red":
                    return Pens.Red;
                case "Green":
                    return Pens.Green;
                default:
                    return Pens.White;
            }
        }
        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);
            angle1 = double.Parse(tbTh1.Text);
            angle2 = double.Parse(tbTh2.Text);
            th1 = angle1 * Math.PI / 180;
            th2 = angle2 * Math.PI / 180;
            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            
            graphics.DrawLine(
                SelectColor(cbColor.Text),
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void cbColor_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private void tbPer1_Scroll(object sender, EventArgs e)
        {       per1 = ((double)tbPer1.Value) / 10;
                lblPer1.Text = per1.ToString();
        }

        private void tbPer2_Scroll(object sender, EventArgs e)
        {
            per2 = ((double)tbPer2.Value) / 10;
            
            lblPer2.Text = per2.ToString();
        }

        private void tbTh1_TextChanged(object sender, EventArgs e)
        {
            bool a = double.TryParse(tbTh1.Text, out angle1);
            bool b = angle1 > 180 || angle1 < 0;

            if (b || !a)
            {
                isOK = false;
                draw.Text = "错误角度，请重新输入";
            }
            else
            {
                isOK = true;
                draw.Text = "";
            }
        }

        private void tbTh2_TextChanged(object sender, EventArgs e)
        {
            bool a = double.TryParse(tbTh2.Text, out angle2);
            bool b = angle2 > 180 || angle2 < 0;

            if (b||!a)
            {
                isOK = false;
                draw.Text = "错误角度，请重新输入";
            }else
            {
                isOK = true;
                draw.Text = "";
            }
        }
    }
}
